package com.lenovo.uitest;

import java.io.IOException;

import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.lenovo.uitest.base.*;

public class UiBase extends UiAutomatorTestCase {
	LenovoUiTestCase base=new LenovoUiTestCase();
	
	/**
	 * ����ͨ��adb shell �����ָ����Ӧ�ó��򣬴����������
	 * @param packagename
	 * @param activity
	 * @return
	 * @throws UiObjectNotFoundException
	 * @throws IOException
	 */
	public boolean launchAppForExec(String packagename, String activity)
			throws UiObjectNotFoundException, IOException {
		String shellcmd = "am start -n " + packagename + "/" + activity;		
		int i=0;
	    while (!packagename.equals(device().getCurrentPackageName())) {
	    	Runtime.getRuntime().exec(shellcmd);
	    	if(i++>=15)break;			
		}		
		return device().getCurrentPackageName().equals(packagename);
	}
	
	/*
	 * �����װ
	 */
	public UiDevice device() {
		return UiDevice.getInstance();	
	}
	public UiSelector selector() {
		return new UiSelector();
	}
	public UiObject object(UiSelector selector) {
		return new UiObject(selector);	
	}
	public UiCollection collection(UiSelector selector) {
		return new UiCollection(selector);
	}
	public UiScrollable scrollable(UiSelector selector) {
		return new UiScrollable(selector);	
	}

	/*
	 * uiselector������װ
	 * selector�࿪ʼѡ������һ��Ϊ���࣬����Ϊ��������
	 */
	public UiSelector text(String text) {
		return selector().textMatches(text);
	}
	public UiSelector desc(String desc) {
		return selector().descriptionMatches(desc);
	}
	public UiSelector classname(String className){
		return selector().classNameMatches(className);
	}
	
	
	public void UnLock() {
		base.Unlock();
	}
}
